<?php if (!defined("_ECRIRE_INC_VERSION")) return;

include_spip('functions/pdk_common');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.ProductModel.php');
 
function formulaires_ecatalogue_product_charger_dist($id_article) {    
	$valeurs = array();
    $p_mdl = pdk_load_class('ProductModel','classes');//new ProductModel();    
    $valeurs = $p_mdl->select_by_id($id_article, true);
    $valeurs['length2'] = $valeurs['length'];
    
	return $valeurs;
}


function formulaires_ecatalogue_product_verifier_dist($id_article){
    $erreurs = array();
    
    $p_mdl = pdk_load_class('ProductModel','classes');    
    $data = $p_mdl->form_data_from_array($_REQUEST);
    $p_mdl->validate($data, $erreurs, true);
         
	return $erreurs;
}


function formulaires_ecatalogue_product_traiter_dist($id_article) {
    $_REQUEST['length'] = _request('length2'); // Solve problem in Opera (doesn't work properly when input has id and name="length")
    $result = array();
    
    $p_mdl = pdk_load_class('ProductModel','classes');
    $comp_mdl = pdk_load_class('CompositeArticleModel','classes');
try {
    
    $data = $p_mdl->form_data_from_array($_REQUEST);
    if (!$data['sold_in_packages']) $data['sold_in_packages '] = 0;
    if (!$p_mdl->update_by_id($id_article, $data, true))
        throw new Exception("Product save fail!");
            
    $result['message_ok'] = _T('ecatalogue:form_saved');
} catch(Exception $e) {
    $result['message_erreur'] = _T('ecatalogue:form_error');
}
    
    set_request('id_component_delete');
    
    return $result;    
}

?>